package net.hn.hnms.biz.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.material.domain.MaterialRecord;
import net.hn.hnms.biz.material.mapper.MaterialRecordMapper;
import net.hn.hnms.biz.material.request.MaterialRecordReq;
import net.hn.hnms.biz.material.service.IMaterialRecordService;
import net.hn.hnms.biz.material.vo.MaterialRecordVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ly
 * 物资台账表服务层实现
 * @date 2022-08-26
 */

@Service
public class MaterialRecordServiceImpl implements IMaterialRecordService {

    private MaterialRecordMapper materialRecordMapper;

    public MaterialRecordServiceImpl(MaterialRecordMapper materialRecordMapper) {
        this.materialRecordMapper = materialRecordMapper;
    }

    /**
     * 物资台账信息分页查询
     * @param req 查询参数
     * @param pageQuery 分页参数
     * @return 物资台账信息
     */
    @Override
    public ResponsePage<MaterialRecordVO> page(MaterialRecordReq req, PageQuery pageQuery) {
        Page<MaterialRecord> page = materialRecordMapper.selectPage(pageQuery.build(), buildQuery(req));

        List<MaterialRecordVO> list =page.getRecords().stream().map(o ->{
            MaterialRecordVO vo = new MaterialRecordVO();
            BeanUtil.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());

        //TODO
        //字典转换
        return new ResponsePage<>(list, (int) page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<MaterialRecord> 查询条件
     */
    private LambdaQueryWrapper<MaterialRecord> buildQuery(MaterialRecordReq req){
        return new LambdaQueryWrapper<MaterialRecord>()
                .like(StrUtil.isNotBlank(req.getMaterialCode()), MaterialRecord::getMaterialCode, req.getMaterialCode())
                .like(StrUtil.isNotBlank(req.getMaterialName()), MaterialRecord::getMaterialName, req.getMaterialName())
                .like(StrUtil.isNotBlank(req.getSpecificationModel()), MaterialRecord::getSpecificationModel, req.getSpecificationModel())
                .like(StrUtil.isNotBlank(req.getSupplier()), MaterialRecord::getSupplier, req.getSupplier())
                .like(StrUtil.isNotBlank(req.getCreatePerson()), MaterialRecord::getCreatePerson, req.getCreatePerson())
                .eq(StrUtil.isNotBlank(req.getWarehouse()), MaterialRecord::getWarehouse, req.getWarehouse())
                .eq(StrUtil.isNotBlank(req.getRecordType()), MaterialRecord::getRecordType, req.getRecordType())
                .eq(StrUtil.isNotBlank(req.getMineCode()), MaterialRecord::getMineCode, req.getMineCode())
                .ge(req.getBeginDate() != null, MaterialRecord::getCreateTime, req.getBeginDate())
                .le(req.getEndDate() != null, MaterialRecord::getCreateTime, req.getEndDate())
                ;
    }
}
